/**
 * Created by Administrator on 2017/8/5.
 */
$(document).ready(function () {
    var addressId='';

    if (C.token==undefined||C.token==null){
        alert('请登录');
        //location.href='login.html';
        againLogin();
        return ;
    }

    var confirmTpl=$('#confirm-order-template').html();
    var confirmCmp=Handlebars.compile(confirmTpl);

    var buyId=localStorage.getItem('orderSubmitId');
    $.ajax({
        url:C.interface.preCreateOrder,
        type:'get',
        dataType:'json',
        data:{
            products:buyId,
            token:C.token
        },
        success:function (response) {
            if (response.result=='success'){
                var data=response.data;
                $('#confirm-order-box').html(confirmCmp(data));
                var noAddress=data.isHaveAddress;
                if (noAddress!='0'){

                    $('.order-address').click(function () {
                        location.href='change_address.html';
                    });
                    $('.con-submit').unbind().bind('click',submitConfirmOrder);
                }else {
                    $('.con-submit').unbind().bind('click',function () {
                        alert('请填写收货地址...');
                    });
                    $('#set-address').click(function () {
                        location.href='addaddress.html';
                    })
                }

            }else if (response.result=='login'){
                alert('请登录');
                //location.href='login.html';
                againLogin();
            }else {
                alert(response.errorMsg);
            }
        }
    });
    
});

function submitConfirmOrder() {

    var loading=dialog.loading();

    addressId=$('.order-address-name').data('addressid');
    
    var goPayId=localStorage.getItem('orderSubmitId');
    var note=$('#taContent').val();
    $.ajax({
        url:C.interface.createOrder,
        type:'post',
        dataType:'json',
        data:{
            products:goPayId,
            addressId:addressId,
            token:C.token,
            note:note
        },
        complete:function () {
            loading.close();
        },
        success:function (response) {
            if (response.result=='success'){

                /*var resourceCartData=JSON.parse(localStorage.getItem('shoppingCard'));

                console.log(resourceCartData);
                var param=localStorage.getItem('orderSubmitId');
                var submitCartData = splitString(param);

                for(var i=0;i<resourceCartData.length;i++){
                    for(var j=0;j<submitCartData.length;j++){
                        if(resourceCartData[i].skuId==submitCartData[j]){
                            resourceCartData.splice(i,1);
                        }
                    }
                }
                localStorage.setItem('shoppingCard',JSON.stringify(resourceCartData));
                */

                localStorage.setItem('orderId',response.data.orderId);
                localStorage.setItem('totalMoney',response.data.totalMoney);

                location.href='confirm-pay.html?version='+C.version;

            }else if (response.result=='login'){
                alert('登录过期请重新登录');
                againLogin();

            }else {
                alert(response.errorMsg);
            }
        }

    })
}

//var prarm=JSON.parse(localStorage.getItem('orderSubmitId'));
var prarm=localStorage.getItem('orderSubmitId');




//截取字符串
function splitString(param) {

    var skus=[];

    var firstSplit=param.split(',');
    //console.log(firstSplit);
    for( var i=0;i<firstSplit.length;i++ ){
        var secondSplit = firstSplit[i].split(":");
        if(secondSplit.length>1) {
            skus[i] = secondSplit[1];
        }

    }
    //console.log(skus);
    return skus;
}











